package com.jctrip.hotel.management.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jctrip.hotel.management.client.FeizhuClient;
import com.jctrip.hotel.management.config.CustomCellWriteWeightConfig;
import com.jctrip.hotel.management.model.param.*;
import com.jctrip.hotel.management.model.result.*;
import com.jctrip.hotel.management.service.CancelOrderManageService;
import com.jctrip.hotel.management.service.GetOrderInfoManageService;
import com.jctrip.hotel.management.service.OrderRecordService;
import com.jctrip.hotel.management.service.PostOrderManageService;
import com.jctrip.hotel.route.model.result.CancelOrderResult;
import com.jctrip.hotel.route.model.result.GetOrderInfoResult;
import com.jctrip.hotel.route.model.result.PostOrderResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags = "management-order-record")
@RestController
@RequestMapping("/management/order/record")
public class OrderRecordController {

    @Autowired
    private OrderRecordService orderRecordService;

    @Autowired
    private PostOrderManageService postOrderManageService;

    @Autowired
    private CancelOrderManageService cancelOrderManageService;

    @Autowired
    private GetOrderInfoManageService getOrderInfoManageService;

    @Autowired
    private FeizhuClient feizhuClient;

    @PostMapping("/listOrder")
    @ApiOperation(value = "listOrder", notes = "获取订单信息列表")
    @ApiOperationSupport(order = 1)
    public Result<GetOrderListResult> listOrder(@RequestBody GetOrderListParam param) {
        return Result.ok(orderRecordService.listOrder(param));
    }

    @ApiOperation(value = "exportXlsx", notes = "导出订单信息列表")
    @PostMapping(value = "/exportXlsx")
    @ApiOperationSupport(order = 2)
    public void exportXlsx(@RequestBody ExportOrderXlsxParam param,
                           HttpServletRequest request) throws IOException {
        orderRecordService.exportExcel(param, request);
    }

    @PostMapping(value = "/postOrder")
    @ApiOperation(value = "postOrder", notes = "下单接口")
    @ApiOperationSupport(order = 3)
    public com.jctrip.hotel.route.model.result.Result<PostOrderResult> postOrder(
            @RequestBody PostOrderParam param) {
        return com.jctrip.hotel.route.model.result.Result.ok(postOrderManageService.execute(param));
    }

    @PostMapping(value = "/cancelOrder")
    @ApiOperation(value = "cancelOrder", notes = "取消订单接口")
    @ApiOperationSupport(order = 4)
    public com.jctrip.hotel.route.model.result.Result<CancelOrderResult> cancelOrder(
            @RequestBody CancelOrderParam param, HttpServletRequest req) {
        return com.jctrip.hotel.route.model.result.Result.ok(cancelOrderManageService.execute(param, req));
    }

    @PostMapping(value = "/getOrderInfo")
    @ApiOperation(value = "getOrderInfo", notes = "获取订单信息接口")
    @ApiOperationSupport(order = 5)
    public com.jctrip.hotel.route.model.result.Result<GetOrderInfoResult> getOrderInfo(
            @RequestBody GetOrderInfoParam param) {
        return com.jctrip.hotel.route.model.result.Result.ok(getOrderInfoManageService.execute(param));
    }

    @PostMapping(value = "/editNotes")
    @ApiOperation(value = "editNotes", notes = "修改订单人工备注")
    @ApiOperationSupport(order = 6)
    public Result<UpdateNotesResult> editNotes(@RequestBody UpdateNotesParam param, HttpServletRequest request) {
        return Result.ok(orderRecordService.editNotes(param, request));
    }

    @PostMapping(value = "/searchPrice")
    @ApiOperation(value = "searchPrice", notes = "查询价格")
    @ApiOperationSupport(order = 7)
    public Result<GetSearchPriceResult> searchPrice(@RequestBody GetPriceParam param) {
        return Result.ok(orderRecordService.searchPriceResult(param));
    }

    @GetMapping(value = "/getLogs")
    @ApiOperation(value = "getLogs", notes = "查找订单日志")
    @ApiOperationSupport(order = 8)
    public Result<List<GetOrderLogResult>> getLogs(@RequestParam("routeId") String routeId,
                                                   @RequestParam("routeOrderId") String orderId) {
        return Result.ok(orderRecordService.getLogs(routeId, orderId));
    }

    @PostMapping(value = "closeRoom")
    public Result<Boolean> closeRoom(@RequestBody UpdateRoomParam param) {
        return feizhuClient.closeRoom(param);
    }

    @GetMapping(value = "/exportList")
    @ApiOperation(value = "exportList", notes = "查找订单日志")
    @ApiOperationSupport(order = 9)
    public Result<GetExportListResult> exportList(HttpServletRequest request,
                                                  @RequestParam(required = false) Long pageNum,
                                                  @RequestParam(required = false) Long pageSize) {
        return Result.ok(orderRecordService.exportList(request, pageNum, pageSize));
    }

    @GetMapping(value = "/download")
    @ApiOperation(value = "download", notes = "查找订单日志")
    @ApiOperationSupport(order = 10)
    public Result<URL> donwload(@RequestParam String id, HttpServletResponse response) {
        return Result.ok(orderRecordService.download(id, response));
    }

    @GetMapping(value = "/deleteExport")
    @ApiOperation(value = "deleteExport", notes = "查找订单日志")
    @ApiOperationSupport(order = 11)
    public Result<String> deleteExport(@RequestParam String id) {
        return Result.ok(orderRecordService.deleteExport(id));
    }
}
